package com.iss.ljx.organ.service.impl;

import com.iss.ljx.duty.repository.DutyDao;
import com.iss.ljx.mybatis.entity.PtDutyEntity;
import com.iss.ljx.mybatis.entity.PtOrganEntity;
import com.iss.ljx.mybatis.flexigrid.PadingRstType;
import com.iss.ljx.mybatis.flexigrid.PagingBean;
import com.iss.ljx.mybatis.ztree.ZtreeEntity;
import com.iss.ljx.organ.entity.SearchOrganEntity;
import com.iss.ljx.organ.repository.OrganDao;
import com.iss.ljx.organ.service.OrganService;
import com.iss.ljx.util.DataUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.UUID;

/**
 * @ClassName OrganSeviceImpl
 * @Description TODO
 * @Author 早起之王
 * @Date 2022/5/17 16:49
 **/
@Service("organService")
public class OrganSeviceImpl implements OrganService {
    @Resource
    private OrganDao organDao;
    @Resource
    private DutyDao dutyDao;
    @Resource
    private MessageSource messageSource;

    @Value("${ubound.uuid}")
    private String uboundUuid;

    @Override
    public PadingRstType<PtOrganEntity> queryOrganListPage(SearchOrganEntity search, PagingBean paging) {
        paging.deal(PtOrganEntity.class);
        PadingRstType<PtOrganEntity> PtOrganEntityPadingRstType = new PadingRstType<PtOrganEntity>();
        PtOrganEntityPadingRstType.setPage(paging.getPage());
        List<PtOrganEntity> list = organDao.queryOrganListPage(search,paging);
        PtOrganEntityPadingRstType.setRawRecords(list);
        Integer total = organDao.queryOrganTotal(search);
        System.out.println(total);
        PtOrganEntityPadingRstType.setTotal(total);
        PtOrganEntityPadingRstType.putItems();
        return PtOrganEntityPadingRstType;
    }

    @Override
    public void insertOrgan(PtOrganEntity ptOrganEntity) {
        String uuid= UUID.randomUUID().toString();
        ptOrganEntity.setOrganUuid(uuid);
        Date time = new Date();
        ptOrganEntity.setModTime(time);
        organDao.insertOrgan(ptOrganEntity);
    }

    @Override
    public void organDelete(String organUuid) {
        organDao.organDelete(organUuid);
    }

    @Override
    public void organUpdate(PtOrganEntity ptOrganEntity) {
        organDao.organUpdate(ptOrganEntity);
    }

    @Value("${root.uuid}")
    private String rootUuid;
    @Override
    public List<ZtreeEntity> getOrganTreeByOrganUuid(String organUuid) {
        List<ZtreeEntity> list = dutyDao.queryMenuTree();
        String parentUuid = organDao.queryOrganTreeByOrganUuid(organUuid);
        if(parentUuid != null){
            int i = 0;
            for(ZtreeEntity item:list){
                if(parentUuid.equals(item.getId())){
                    item.setChecked(true);
                }
                if(i > 10 ){
                    item.setOpen(false);
                }
                i++;
            }
        }

        ZtreeEntity ZtreeEntity = new ZtreeEntity();
        ZtreeEntity.setId(rootUuid);
        Locale locale = LocaleContextHolder.getLocale();
        String rootName = messageSource.getMessage("common.root.node.name",null,locale);
        ZtreeEntity.setName(rootName);
        list.add(ZtreeEntity);
        return list;
    }

    @Override
    public void boundOrgan(String organUuid, String parentUuid) {
        organDao.updateParentUuidByOrganUuid(organUuid,parentUuid);
    }

    @Override
    public void unBoundByOrganUuid(String organUuid) {
        organDao.updateParentUuidByOrganUuid(organUuid,uboundUuid);
    }

    @Override
    public PtOrganEntity getOrganInfoByUuid(String organUuid) {
        return organDao.getOrganInfoByUuid(organUuid);
    }


}
